package com.yiren.dbaa.module.practice_set.carmodel.view.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yiren.backstage.currency.entity.CarModelEntity;
import com.yiren.backstage.currency.utils.DateUtil;
import com.yiren.dbaa.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author lwb
 * @date : 2022/12/6 16:18
 * @description: 车模版本记录适配器
 */

public class CarModelVersionAdapter extends BaseAdapter {

    private Context context;
    private List<CarModelEntity> list;
    public int mSelect = 0;

    public CarModelVersionAdapter(Context context, List<CarModelEntity> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = View.inflate(context, R.layout.list_carmodel_version, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tvVersion.setText("V" + list.get(position).getCarModelVer());
        viewHolder.tvReleasetime.setText(DateUtil.longTimeToString(list.get(position).getPublishTime()));
        viewHolder.tvUsingRange.setText(list.get(position).getUseRange() == 1 ? "个别用户" : "全部用户");
        viewHolder.tvUpdatedetail.setText(list.get(position).getUpdateDetails());
        viewHolder.tvOperation.setText("下载");
        viewHolder.tvOperation.setTextColor(context.getResources().getColor(R.color.dbaf_blue));
        viewHolder.tvOperation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnManageClickListener != null) {
                    mOnManageClickListener.onManageClick(position);
                }
            }
        });
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.tv_version)
        TextView tvVersion;
        @BindView(R.id.tv_releasetime)
        TextView tvReleasetime;
        @BindView(R.id.tv_using_range)
        TextView tvUsingRange;
        @BindView(R.id.tv_updatedetail)
        TextView tvUpdatedetail;
        @BindView(R.id.tv_operation)
        TextView tvOperation;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    //启用停用回调
    public interface OnManageClickListener {

        void onManageClick(int position);
    }

    private OnManageClickListener mOnManageClickListener;

    public void setOnManageClickListener(OnManageClickListener onManageClickListener) {
        mOnManageClickListener = onManageClickListener;
    }

    public void changeSelected(int positon) {
        //刷新方法
        if (positon != mSelect) {
            mSelect = positon;
            notifyDataSetChanged();
        }
    }

}